/*
 * @(#)cn.design.pattern.adapter.springmvc 2020/4/15/0015
 *
 * Copy Right@ 人人译视界
 */


package cn.design.pattern.adapter.springmvc;

import java.util.LinkedList;
import java.util.List;

/**
 * <pre>
 * @author tuwei
 *
 * @version 0.9
 *
 * 修改版本: 0.9
 * 修改日期: 2020/4/15/0015
 * 修改人 :  tuwei
 * 修改说明: 初步完成
 * 复审人 ：
 * </pre>
 */
public class DispachServlet {
	private final List<HandlerAdapter> handlerAdapters = new LinkedList<>();

	public DispachServlet() {
		handlerAdapters.add(new AnnotationHandlerAdapter());
		handlerAdapters.add(new SimpleHandlerAdapter());
		handlerAdapters.add(new HttpHandlerAdapter());
	}

	public void doDispatch() {
		HttpController http = new HttpController();
		HandlerAdapter handlerAdapter = getHandler(http);
		handlerAdapter.handle(http);
	}

	public HandlerAdapter getHandler(Controller controller) {
		for (HandlerAdapter adapter : this.handlerAdapters) {
			if (adapter.supports(controller)) {
				return adapter;
			}
		}
		return null;
	}

	public static void main(String[] args) {
		new DispachServlet().doDispatch();
	}
}
